package com.weijing.pretty_app.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.jeremyliao.liveeventbus.LiveEventBus
import com.weijing.pretty_app.R
import com.weijing.pretty_app.databinding.ActivityTuoMaoModelBinding
import com.weijing.pretty_app.models.SelectModel
import com.weijing.pretty_app.util.BluetoothSocketManager
import com.weijing.pretty_app.util.HexConverter
import com.weijing.pretty_app.util.KeyUtil

private const val TAG = "TuoMaoModelActivity"

/**
 * 脱毛模式选择页面
 * 智能模式 or 专家模式
 */
class TuoMaoModelActivity : BaseActivity() {

    private val binding by lazy { ActivityTuoMaoModelBinding.inflate(layoutInflater) }

    override fun getMainView(): View = binding.main

    private var selectMode: SelectModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        initClick()
    }

    private fun initClick() {
        binding.tuoMaoModeIvBack.setOnClickListener { finish() }

        //智能模式 smart_mode
        binding.tuoMaoModeLl1.setOnClickListener {
            val newModel =
                SelectModel(
                    model1 = selectMode?.model1,
                    model2 = getString(R.string.smart_mode_low)
                )
            LiveEventBus.get<SelectModel>(KeyUtil.TO_NEXT_ACTIVITY).post(newModel)
            val intent = Intent(this@TuoMaoModelActivity, TuoMaoNextActivity::class.java)
            startActivity(intent)
        }

        //专家模式 expert_mode
        binding.tuoMaoModeLl2.setOnClickListener {
            val newModel = SelectModel(
                model1 = selectMode?.model1, model2 = getString(R.string.expert_mode_low)
            )
            LiveEventBus.get<Map<Int, SelectModel>>(KeyUtil.TO_HOME_ACTIVITY)
                .post(mapOf(0 to newModel))
            sendData()
            val intent = Intent(this@TuoMaoModelActivity, HomeActivity::class.java)
            startActivity(intent)
        }

        LiveEventBus.get<SelectModel>(KeyUtil.TO_TuoMaoModelActivity).observeSticky(this, { it ->
            selectMode = it
        })
    }

    private fun sendData() {
        val command = "AA 55 E3 02 01 01 EE"
        Log.i(TAG, "脱毛专家-- $command")
        val data = HexConverter.hexStringToByteArray(command)
        BluetoothSocketManager.send(data)
    }
}